<?php

/**
 * 应用名_模块名
 */

require_library('framework.DataAccess');

class user {

    static public function getItem($id)
    {
        $sql = 'SELECT * FROM users WHERE user_id = ' . intval($id);
        return DataAccess::select('demo.user.users')->getRow($sql);
    }

    static public function getItems($groupId = NULL, $parentId = NULL)
    {
        $sql = 'SELECT * FROM users WHERE 1';
        if ($groupId !== NULL)
        {
            $sql .= ' AND group_id = ' . intval($groupId);
        }
        if ($parentId !== NULL)
        {
            $sql .= ' AND parent_id = ' . intval($parentId);
        }

        return DataAccess::select('demo.user.users')->getAll($sql);
    }

    static public function getItemById($ids)
    {
        if (!is_array($ids))
        {
            throw new Exception('invalid args');
        }

        $sql = 'SELECT * FROM users WHERE user_id IN(' . implode(',', $ids) . ')';
        
        //return array_spec_key(DataAccess::table('demo.user')->getAll($sql), 'user_id');
        return DataAccess::select('demo.user.users')->getAll($sql, 'user_id');
    }

    static public function addItem($data)
    {
        if (!is_array($ids))
        {
            throw new Exception('invalid args');
        }

        return DataAccess::update('demo.user.users')->query($data);
    }

    static public function updateItem($id, $data)
    {
        if (!is_array($ids))
        {
            throw new Exception('invalid args');
        }

        $condition = array('user_id' => $id);
        
        return DataAccess::update('demo.user.users')->query($condition, $data);
    }

    static public function deleteItem($id)
    {
        if (!is_array($ids))
        {
            throw new Exception('invalid args');
        }

        $condition = array('user_id' => $id);
        
        return DataAccess::update('demo.user.users')->query($condition);
    }
}

?>
